package com.far.game.xiuzhen.gm.web.api.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.far.game.xiuzhen.common.database.entity.Servers;

import com.far.game.xiuzhen.common.database.entity.Vo.admin.ServersVo;
import com.far.game.xiuzhen.gm.web.BaseController;
import com.far.game.xiuzhen.service.IServersService;

import com.far.tools.net.http.ReqParamWrapper;
import com.far.tools.net.http.transfer.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mike
 * @since 2022-07-15
 */
@Api(tags = "游戏服务层")

@RestController
@RequestMapping("/api/admin/servers")
public class ServersController extends BaseController {

    @Autowired
    @Qualifier("serversServiceImpl")
    private IServersService iServersService;


    /**
     * 游戏服务器表数据新增
     */
    @ApiOperation(value = "游戏服务器新增", notes = "新增游戏服务器表数据")
    @PostMapping(value = "servers-add")
    private ResultData<String> serversAdd(@RequestBody Servers servers) {
        servers.setUpdateBy(this.getVisitor().getUserName());
        servers.setCreateBy(this.getVisitor().getUserName());
        if (iServersService.save(servers)) {
            return ResultData.ok("添加成功");
        }
        return ResultData.error("添加失败");
    }

    /**
     * 游戏服务器表数据更新
     */
    @ApiOperation(value = "游戏服务器更新", notes = "更新游戏服务器表数据")
    @PostMapping(value = "servers-update")
    private ResultData<String> serversUpdate(@RequestBody Servers Servers) {

        Servers.setUpdateBy(this.getVisitor().getUserName());
        if (iServersService.updateById(Servers)) {
            return ResultData.ok("更新成功");
        }
        return ResultData.error("更新失败");
    }

    /**
     * 游戏服务器表数据删除
     */
    @ApiOperation(value = "游戏服务器删除", notes = "删除游戏服务器表数据")
    @ApiImplicitParam(name = "tid", value = "ID", required = true, paramType = "query", dataType = "Integer")
    @GetMapping(value = "servers-remove")
    private ResultData<String> serversRemove(@RequestParam("tid") Integer tid) {
        if (iServersService.removeById(tid)) {
            return ResultData.ok("删除成功");
        }
        return ResultData.error("删除失败");
    }

    /**
     * 游戏服务器表数据查询
     */
    @ApiOperation(value = "游戏服务器查询", notes = "ReqParamWrapper")
    @PostMapping(value = "servers-query")

    private ResultData<IPage<Servers>> serversQuery(@RequestBody ReqParamWrapper<Servers, String> reqParamWrapper) {

        IPage<Servers> IPage = reqParamWrapper.pageLists(iServersService);
        if (IPage.getRecords().size() == 0) {
            return ResultData.error("暂无数据");
        }
        return ResultData.ok(IPage);
    }


    /**
     * 游戏服务器表数据查询
     */
    @ApiOperation(value = "所在区查询", notes = "查询游戏服务器所有区")
    @PostMapping(value = "domain-name-query")

    private ResultData<List<ServersVo>> domainNameQuery() {

        List<Servers> domain = iServersService.list();
        List<ServersVo> serversVos = new ArrayList<>();
        for (Servers servers : domain) {
            ServersVo serversVo = new ServersVo();
            BeanUtil.copyProperties(servers, serversVo);
            Servers servers1 = iServersService.lambdaQuery().eq(Servers::getTid, servers.getParentTid()).one();
            if (ObjectUtil.isEmpty(servers1)) {
                serversVo.setDomainName(null);
            } else {
                serversVo.setDomainName(servers1.getServerName());

            }
            serversVos.add(serversVo);
        }

        return ResultData.ok(serversVos);
    }


}

